/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.sdp;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import telsoft.util.StringEscapeUtil;
import telsoft.util.StringUtil;

/**
 *
 * @author tuanta
 */
public class VioletSDPSubRegistrator {
    public static void register(String productCode,String serviceID) throws Exception{
    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod("http://mobistore.vn/portal/XHTML/violet.portal");
    /* fixed params */
    postMethod.addParameter("p_p_id", "Search");
    postMethod.addParameter("p_p_p_id", "31");
    postMethod.addParameter("p_p_state", "maximized");
    postMethod.addParameter("p_p_mode", "view");
    postMethod.addParameter("p_l_id", "22");
    postMethod.addParameter("p_u_type", "action");
    postMethod.addParameter("NAMESPACE_31struts.portlet.mode", "view");
    postMethod.addParameter("NAMESPACE_31dynamicAttributes", "{}");
    postMethod.addParameter("NAMESPACE_31templateDir", "template");
    postMethod.addParameter("NAMESPACE_31theme", "xhtml");
    postMethod.addParameter("NAMESPACE_31struts.portlet.action", "/contentoperate/subscribeProductByWap.action");
    /* dynamic params */
    postMethod.addParameter("productID", productCode);
    postMethod.addParameter("contentID", serviceID);
    postMethod.addParameter("contentCode", serviceID);
    postMethod.addParameter("productType", "service");
    httpClient.executeMethod(postMethod);
    if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
            try {
                String resp = postMethod.getResponseBodyAsString();
            } catch (IOException ex) {
                Logger.getLogger(VioletSDPSubRegistrator.class.getName()).log(Level.SEVERE, null, ex);
            }
    } else {
        String location = postMethod.getResponseHeader("Location").toString();
         throw new Exception("URL returned code:" + postMethod.getStatusCode()+ ", location:" + location);
    }
     
}
    public static void main(String[] args) {
        String strURL = StringEscapeUtil.unescapeHtml("http://mobistore.vn/portal/XHTML/violet.portal?p_p_id=Search&p_p_p_id=31&p_p_state=maximized&p_p_mode=view&p_l_id=22&p_u_type=action&NAMESPACE_31struts.portlet.mode=view&NAMESPACE_31dynamicAttributes={}&NAMESPACE_31templateDir=template&NAMESPACE_31theme=xhtml&NAMESPACE_31struts.portlet.action=/contentoperate/subscribeProductByWap.action");
//        System.out.println(strURL);
    }
}
